package com.sjq.supplier.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sjq.supplier.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.sjq.supplier.entity.Admininfo;
import com.sjq.supplier.service.AdmininfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * (Admininfo)表控制层
 *
 * @author makejava
 * @since 2024-01-02 14:54:49
 */
@Slf4j
@RestController
@RequestMapping("admininfo")
public class AdmininfoController {
    /**
     * 服务对象
     */
    @Resource
    private AdmininfoService admininfoService;

    /**
     * 分页查询所有数据
     *
     * @param admininfo 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Admininfo admininfo, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        LambdaQueryWrapper<Admininfo> q = new LambdaQueryWrapper<>();
        q.like(StringUtils.hasLength(admininfo.getUsername()), Admininfo::getUsername, admininfo.getUsername());
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", admininfoService.list(q).size());
        //分页
        q.last("limit " + (page - 1) * limit + "," + limit);
        result.put("data", admininfoService.list(q));
        return Result.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }
        return Result.success(admininfoService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param admininfo 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public Result insert(Admininfo admininfo) {
        //判断admininfo是否为空
        if (admininfo == null) {
            return Result.failure("数据不能为空");
        }

        //转换成大写
        admininfo.setAccount(admininfo.getAccount().toUpperCase());

        //判断账号是否存在
        if (admininfoService.getOne(new LambdaQueryWrapper<Admininfo>().eq(Admininfo::getAccount, admininfo.getAccount())) != null) {
            return Result.failure("账号已存在");
        }

        admininfo.setIsvalid(1);
        admininfo.setRole(1);
        if (admininfoService.save(admininfo)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 修改数据
     *
     * @param admininfo 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    @Transactional(rollbackFor = Exception.class)
    public Result update(Admininfo admininfo) {
        //判断admininfo是否为空
        if (admininfo == null) {
            return Result.failure("数据不能为空");
        }

        if (admininfoService.updateById(admininfo)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> delete(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        Admininfo admininfo = new Admininfo();
        admininfo.setIsvalid(2);
        admininfo.setId(id);

        if (admininfoService.removeById(admininfo)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }
}

